package com.zmj.sy.mom.srv.aps.utils;

import org.apache.poi.ss.usermodel.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PoiUtils {

    public static Row title(Sheet sheet, List<String> titles, List<CellStyle> styles) {
        Row row = sheet.createRow(0);
        for (int i = 0; i < titles.size(); i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(titles.get(i));
            if (styles != null && styles.size() > i) {
                cell.setCellStyle(styles.get(i));
            }
        }
        return row;
    }

    public static Row title(Sheet sheet, List<String> titles) {
        return title(sheet, titles, null);
    }

    public static List<Cell> cells(Workbook workbook, Sheet sheet, int rowIndex, boolean center, Object... values) {

        Row currentRow = sheet.createRow(rowIndex);

        List<Cell> cells = new ArrayList<>();
        for (int i = 0; i < values.length; i++) {
            CellStyle style = workbook.createCellStyle();
            // 下边框
            style.setBorderBottom(BorderStyle.THIN);
            // 上边框
            style.setBorderTop(BorderStyle.THIN);
            // 左边框
            style.setBorderLeft(BorderStyle.THIN);
            // 右边框
            style.setBorderRight(BorderStyle.THIN);
            if (center) {
                style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
                style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
            }
            Cell cell = currentRow.createCell(i);

            if(values[i] instanceof BigDecimal){
                style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
                style.setAlignment(HorizontalAlignment.RIGHT); // 水平居中
                cell.setCellStyle(style);
                cell.setCellValue(((BigDecimal)values[i]).doubleValue());
            } else if(values[i] instanceof Integer){
                style.setDataFormat(workbook.createDataFormat().getFormat("0"));
                cell.setCellStyle(style);
                cell.setCellValue((Integer) values[i]);
            } else if(values[i] instanceof Long){
                style.setDataFormat(workbook.createDataFormat().getFormat("0"));
                cell.setCellStyle(style);
                cell.setCellValue((Long) values[i]);
            } else if(values[i] instanceof Double){
                style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
                style.setAlignment(HorizontalAlignment.RIGHT); // 水平居中
                cell.setCellStyle(style);
                cell.setCellValue((Double) values[i]);
            } else if(values[i] instanceof LocalDate){
                style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
                cell.setCellStyle(style);
                cell.setCellValue(((LocalDate)values[i]));
            } else if(values[i] instanceof LocalDateTime){
                style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
                cell.setCellStyle(style);
                cell.setCellValue(((LocalDateTime)values[i]));

            } else {
                cell.setCellValue(values[i].toString());
            }

            cell.setCellStyle(style);
            cells.add(cell);
        }
        return cells;
    }

    public static List<Cell> cells(Workbook workbook, Sheet sheet, int rowIndex, boolean center, Map<Integer,Short> backColor, Map<Integer, Short> wordColor, Object... values) {

        Row currentRow = sheet.createRow(rowIndex);

        List<Cell> cells = new ArrayList<>();
        for (int i = 0; i < values.length; i++) {
            CellStyle style = workbook.createCellStyle();
            // 下边框
            style.setBorderBottom(BorderStyle.THIN);
            // 上边框
            style.setBorderTop(BorderStyle.THIN);
            // 左边框
            style.setBorderLeft(BorderStyle.THIN);
            // 右边框
            style.setBorderRight(BorderStyle.THIN);
            if (center) {
                style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
                style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
            }
            Cell cell = currentRow.createCell(i);

            currentRow.setHeightInPoints(20); // 行高
            if (backColor != null && backColor.get(i) != null) {
                style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 背景色填充方式
                style.setFillForegroundColor(backColor.get(i)); // 背景色
            }
            if (wordColor != null && wordColor.get(i) != null) {
                Font font = workbook.createFont(); // 字体样式
                font.setColor(wordColor.get(i)); // 字体颜色
                style.setFont(font); // 设置应用
            }

            if(values[i] instanceof BigDecimal){
                style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
                style.setAlignment(HorizontalAlignment.RIGHT); // 水平居中
                cell.setCellStyle(style);
                cell.setCellValue(((BigDecimal)values[i]).doubleValue());
            } else if(values[i] instanceof Integer){
                style.setDataFormat(workbook.createDataFormat().getFormat("0"));
                cell.setCellStyle(style);
                cell.setCellValue((Integer) values[i]);
            } else if(values[i] instanceof Long){
                style.setDataFormat(workbook.createDataFormat().getFormat("0"));
                cell.setCellStyle(style);
                cell.setCellValue((Long) values[i]);
            } else if(values[i] instanceof Double){
                style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
                style.setAlignment(HorizontalAlignment.RIGHT); // 水平居中
                cell.setCellStyle(style);
                cell.setCellValue((Double) values[i]);
            } else if(values[i] instanceof LocalDate){
                style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
                cell.setCellStyle(style);
                cell.setCellValue(((LocalDate)values[i]));
            } else if(values[i] instanceof LocalDateTime){
                style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
                cell.setCellStyle(style);
                cell.setCellValue(((LocalDateTime)values[i]));

            } else {
                cell.setCellValue(values[i].toString());
            }

            cell.setCellStyle(style);
            cells.add(cell);
        }
        return cells;
    }

    public static List<Cell> tableName(Workbook workbook, Sheet sheet, int rowIndex, boolean center, Integer height, int cellIndex, Object... values) {

        Row currentRow = sheet.createRow(rowIndex);

        List<Cell> cells = new ArrayList<>();
        for (int i = 0; i < values.length; i++) {
            CellStyle style = workbook.createCellStyle();
            // 下边框
            style.setBorderBottom(BorderStyle.THIN);
            // 上边框
            style.setBorderTop(BorderStyle.THIN);
            // 左边框
            style.setBorderLeft(BorderStyle.THIN);
            // 右边框
            style.setBorderRight(BorderStyle.THIN);
            if (center) {
                style.setAlignment(HorizontalAlignment.CENTER); // 水平居中
                style.setVerticalAlignment(VerticalAlignment.CENTER); // 垂直居中
            }
            Cell cell = currentRow.createCell(i);

            if (i<=cellIndex) {
                Font font = workbook.createFont(); // 字体样式
                // 设置字体大小（单位：磅）
                font.setFontHeightInPoints((short) 20); // 设置字体大小为14磅
                // 设置加粗
                font.setBold(true); // true为加粗，false为正常
                // 设置字体名称
                font.setFontName("微软雅黑");
                style.setFont(font); // 设置应用
            }
            currentRow.setHeightInPoints(height); // 行高

            if(values[i] instanceof BigDecimal){
                style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
                style.setAlignment(HorizontalAlignment.RIGHT); // 水平居中
                cell.setCellStyle(style);
                cell.setCellValue(((BigDecimal)values[i]).doubleValue());
            } else if(values[i] instanceof Integer){
                style.setDataFormat(workbook.createDataFormat().getFormat("0"));
                cell.setCellStyle(style);
                cell.setCellValue((Integer) values[i]);
            } else if(values[i] instanceof Long){
                style.setDataFormat(workbook.createDataFormat().getFormat("0"));
                cell.setCellStyle(style);
                cell.setCellValue((Long) values[i]);
            } else if(values[i] instanceof Double){
                style.setDataFormat(workbook.createDataFormat().getFormat("0.00"));
                style.setAlignment(HorizontalAlignment.RIGHT); // 水平居中
                cell.setCellStyle(style);
                cell.setCellValue((Double) values[i]);
            } else if(values[i] instanceof LocalDate){
                style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
                cell.setCellStyle(style);
                cell.setCellValue(((LocalDate)values[i]));
            } else if(values[i] instanceof LocalDateTime){
                style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
                cell.setCellStyle(style);
                cell.setCellValue(((LocalDateTime)values[i]));

            } else {
                cell.setCellValue(values[i].toString());
            }

            cell.setCellStyle(style);
            cells.add(cell);
        }
        return cells;
    }

    public static List<Cell> cells(Row row, String... values) {

        List<Cell> cells = new ArrayList<>();
        for (int i = 0; i < values.length; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(values[i]);
            cells.add(cell);
        }
        return cells;
    }

    public static List<Cell> cells(Row row, List<String> values) {

        List<Cell> cells = new ArrayList<>();
        for (int i = 0; i < values.size(); i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(values.get(i));
            cells.add(cell);
        }
        return cells;
    }

}
